package com.loongtech.bi.manager.ios;

import com.loongtech.bi.entity.ios.EntityDayProductManager;
import com.loongtech.bi.entity.ios.EntityMonthProductManager;
import com.loongtech.bi.entity.ios.EntityProductManager;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IOSBillDataManager extends ManagerQueryCacheBase<Object> {

	private static final long serialVersionUID = 2L;

	@Autowired
    private DayProductManager dayProductManager;
	@Autowired
    private ProductManager productManager;
	@Autowired
    private MonthProductManager monthProductManager;

	public Map<String, BillData> getBillDateDatas(Date dateStart, Date dateEnd, List<String> productids, List<String> dates) {
        Map<String, BillData> result = new LinkedHashMap<>();

        Map<String, String> pn = new LinkedHashMap<>();
        Map<String, Integer> pc = new LinkedHashMap<>();
        List<EntityProductManager> ps = productManager.findAll();
        for (EntityProductManager p : ps) {
            productids.add(p.getName());
            pn.put(p.getProductid(), p.getName());
            pc.put(p.getProductid(), Integer.parseInt(p.getPay()));
        }

        List<EntityDayProductManager> dp = dayProductManager.getDayProducts(dateStart, dateEnd);
        for (EntityDayProductManager e : dp) {
            String dateStr = e.getLookDate();
            String pid = e.getProductid();
            String name = pn.get(pid);
            int pay = pc.get(pid);
            int in = Integer.parseInt(e.getCost()) * pay;
            int ret = Integer.parseInt(e.getUnsubscribe()) * pay;

            BillData d = new BillData();
            d.setProductid(name);
            d.setBi(0);  // TODO
            d.setIosin(in);
            d.setIosreturn(ret);
            d.setIosPerBi((float)(d.getIosin() == 0 ? 0 : d.getIosreturn() * 1.0 / d.getIosin()));

            if (!dates.contains(dateStr)) {
                dates.add(dateStr);
            }

            result.put(dateStr + "-" + name, d);

            BillData tall = result.get("all" + "-" + name);
            if (tall == null) {
                tall = new BillData();
                tall.setProductid(name);
                result.put("all" + "-" + name, tall);
            }
            tall.setBi(tall.getBi() + d.getBi());
            tall.setIosin(tall.getIosin() + d.getIosin());
            tall.setIosreturn(tall.getIosreturn() + d.getIosreturn());
            tall.setIosPerBi((float)(tall.getIosin() == 0 ? 0 : tall.getIosreturn() * 1.0 / tall.getIosin()));
        }

        return result;
    }

    public class BillData {
        private String productid;  // 产品ID
        private int bi;  // bi入款
        private int iosin;  // ios销售数
        private int iosreturn;  // ios退订数
        private float iosPerBi;  // 退款占比

        public String getProductid() {
            return productid;
        }

        public void setProductid(String productid) {
            this.productid = productid;
        }

        public int getBi() {
            return bi;
        }

        public void setBi(int bi) {
            this.bi = bi;
        }

        public int getIosin() {
            return iosin;
        }

        public void setIosin(int iosin) {
            this.iosin = iosin;
        }

        public int getIosreturn() {
            return iosreturn;
        }

        public void setIosreturn(int iosreturn) {
            this.iosreturn = iosreturn;
        }

        public float getIosPerBi() {
            return iosPerBi;
        }

        public void setIosPerBi(float iosPerBi) {
            this.iosPerBi = iosPerBi;
        }
    }






    public List<BillAllData> getBillAllDatas(Date dateStart, Date dateEnd) {
        List<BillAllData> result = new ArrayList<>();
        Map<String, BillAllData> tmp = new LinkedHashMap<>();

        Map<String, String> pn = new LinkedHashMap<>();
        Map<String, Integer> pc = new LinkedHashMap<>();
        List<EntityProductManager> ps = productManager.findAll();
        for (EntityProductManager p : ps) {
            pn.put(p.getProductid(), p.getName());
            pc.put(p.getProductid(), Integer.parseInt(p.getPay()));
        }

        List<EntityDayProductManager> dp = dayProductManager.getDayProducts(dateStart, dateEnd);
        for (EntityDayProductManager e : dp) {
            String pid = e.getProductid();
            int pay = pc.get(pid);
            int in = Integer.parseInt(e.getCost()) * pay;
            int ret = Integer.parseInt(e.getUnsubscribe()) * pay;

            BillAllData t = tmp.get(pid);
            if (t == null) {
                t = new BillAllData();
                t.setProductid(pid);
                t.setProductname(pn.get(pid));
                tmp.put(pid, t);
            }
            t.setBi(0);  // TODO
            t.setIosin(t.getIosin() + in);
            t.setIosreturn(t.getIosreturn() + ret);
            t.setIosPerBi((float)(t.getIosin() == 0 ? 0 : t.getIosreturn() * 1.0 / t.getIosin()));
        }

        result.addAll(tmp.values());
        return result;
    }


    public class BillAllData {
        private String productid;  // 产品ID
        private String productname;  // 产品NAME
        private int bi;  // bi入款
        private int iosin;  // ios销售数
        private int iosreturn;  // ios退订数
        private float iosPerBi;  // 退款占比

        public String getProductid() {
            return productid;
        }

        public void setProductid(String productid) {
            this.productid = productid;
        }

        public int getBi() {
            return bi;
        }

        public void setBi(int bi) {
            this.bi = bi;
        }

        public int getIosin() {
            return iosin;
        }

        public void setIosin(int iosin) {
            this.iosin = iosin;
        }

        public int getIosreturn() {
            return iosreturn;
        }

        public void setIosreturn(int iosreturn) {
            this.iosreturn = iosreturn;
        }

        public float getIosPerBi() {
            return iosPerBi;
        }

        public void setIosPerBi(float iosPerBi) {
            this.iosPerBi = iosPerBi;
        }

        public String getProductname() {
            return productname;
        }

        public void setProductname(String productname) {
            this.productname = productname;
        }
    }


    public List<BillMonthData> getBillMonthDatas(Date monthStart, Date monthEnd) {
        List<BillMonthData> result = new ArrayList<>();
        Map<String, BillMonthData> tmp = new LinkedHashMap<>();

        Map<String, String> pn = new LinkedHashMap<>();
        Map<String, Integer> pc = new LinkedHashMap<>();
        List<EntityProductManager> ps = productManager.findAll();
        for (EntityProductManager p : ps) {
            pn.put(p.getProductid(), p.getName());
            pc.put(p.getProductid(), Integer.parseInt(p.getPay()));
        }

        List<EntityMonthProductManager> startList = monthProductManager.getMonthProducts(monthStart);
        List<EntityMonthProductManager> endList = monthProductManager.getMonthProducts(monthEnd);

        for (EntityMonthProductManager e : startList) {
            String pid = e.getProductid();
            int pay = pc.get(pid);
            int in = Integer.parseInt(e.getCost()) * pay;
            int ret = Integer.parseInt(e.getUnsubscribe()) * pay;

            BillMonthData t = tmp.get(pid);
            if (t == null) {
                t = new BillMonthData();
                t.setProductid(pid);
                t.setProductname(pn.get(pid));
                tmp.put(pid, t);
            }
            t.setMonth(Time.getMonthStr(monthStart));
            t.setBi(0);  // TODO
            t.setIosin(t.getIosin() + in);
            t.setIosreturn(t.getIosreturn() + ret);
            t.setIosPerBi((float)(t.getIosin() == 0 ? 0 : t.getIosreturn() * 1.0 / t.getIosin()));
        }
        for (EntityMonthProductManager e : endList) {
            String pid = e.getProductid();
            int pay = pc.get(pid);
            int in = Integer.parseInt(e.getCost()) * pay;
            int ret = Integer.parseInt(e.getUnsubscribe()) * pay;

            BillMonthData t = tmp.get(pid);
            if (t == null) {
                t = new BillMonthData();
                t.setProductid(pid);
                t.setProductname(pn.get(pid));
                tmp.put(pid, t);
            }
            t.setMonth1(Time.getMonthStr(monthEnd));
            t.setBi2(0);  // TODO
            t.setIosin2(t.getIosin() + in);
            t.setIosreturn2(t.getIosreturn() + ret);
            t.setIosPerBi2((float)(t.getIosin() == 0 ? 0 : t.getIosreturn() * 1.0 / t.getIosin()));
        }

        result.addAll(tmp.values());
        return result;
    }



    public class BillMonthData {
        private String productid;  // 产品ID
        private String productname;  // 产品NAME
        private String month;
        private String month1;

        private int bi;  // bi入款
        private int iosin;  // ios销售数
        private int iosreturn;  // ios退订数
        private float iosPerBi;  // 退款占比
        private String biStr;
        private String iosinStr;
        private String insreturnStr;
        private String iosPerBiStr;

        private int bi2;  // bi入款
        private int iosin2;  // ios销售数
        private int iosreturn2;  // ios退订数
        private float iosPerBi2;  // 退款占比

        private String biStr2;
        private String iosinStr2;
        private String insreturnStr2;
        private String iosPerBiStr2;

        public String getProductid() {
            return productid;
        }

        public void setProductid(String productid) {
            this.productid = productid;
        }

        public int getBi() {
            return bi;
        }

        public void setBi(int bi) {
            this.bi = bi;
        }

        public int getIosin() {
            return iosin;
        }

        public void setIosin(int iosin) {
            this.iosin = iosin;
        }

        public int getIosreturn() {
            return iosreturn;
        }

        public void setIosreturn(int iosreturn) {
            this.iosreturn = iosreturn;
        }

        public float getIosPerBi() {
            return iosPerBi;
        }

        public void setIosPerBi(float iosPerBi) {
            this.iosPerBi = iosPerBi;
        }

        public String getProductname() {
            return productname;
        }

        public void setProductname(String productname) {
            this.productname = productname;
        }

        public int getBi2() {
            return bi2;
        }

        public void setBi2(int bi2) {
            this.bi2 = bi2;
        }

        public int getIosin2() {
            return iosin2;
        }

        public void setIosin2(int iosin2) {
            this.iosin2 = iosin2;
        }

        public int getIosreturn2() {
            return iosreturn2;
        }

        public void setIosreturn2(int iosreturn2) {
            this.iosreturn2 = iosreturn2;
        }

        public float getIosPerBi2() {
            return iosPerBi2;
        }

        public void setIosPerBi2(float iosPerBi2) {
            this.iosPerBi2 = iosPerBi2;
        }

        public String getMonth() {
            return month;
        }

        public void setMonth(String month) {
            this.month = month;
            this.biStr = month + " BI收入（美分）";
            this.iosinStr = month + " IOS收入（美分）";
            this.insreturnStr = month + " BI退款（美分）";
            this.iosPerBiStr = month + " 退款比例";
        }

        public String getMonth1() {
            return month1;
        }

        public void setMonth1(String month1) {
            this.month1 = month;
            this.biStr2 = month + " BI收入（美分）";
            this.iosinStr2 = month + " IOS收入（美分）";
            this.insreturnStr2 = month + " BI退款（美分）";
            this.iosPerBiStr2 = month + " 退款比例";
        }

        public String getBiStr() {
            return biStr;
        }

        public void setBiStr(String biStr) {
            this.biStr = biStr;
        }

        public String getIosinStr() {
            return iosinStr;
        }

        public void setIosinStr(String iosinStr) {
            this.iosinStr = iosinStr;
        }

        public String getInsreturnStr() {
            return insreturnStr;
        }

        public void setInsreturnStr(String insreturnStr) {
            this.insreturnStr = insreturnStr;
        }

        public String getIosPerBiStr() {
            return iosPerBiStr;
        }

        public void setIosPerBiStr(String iosPerBiStr) {
            this.iosPerBiStr = iosPerBiStr;
        }

        public String getBiStr2() {
            return biStr2;
        }

        public void setBiStr2(String biStr2) {
            this.biStr2 = biStr2;
        }

        public String getIosinStr2() {
            return iosinStr2;
        }

        public void setIosinStr2(String iosinStr2) {
            this.iosinStr2 = iosinStr2;
        }

        public String getInsreturnStr2() {
            return insreturnStr2;
        }

        public void setInsreturnStr2(String insreturnStr2) {
            this.insreturnStr2 = insreturnStr2;
        }

        public String getIosPerBiStr2() {
            return iosPerBiStr2;
        }

        public void setIosPerBiStr2(String iosPerBiStr2) {
            this.iosPerBiStr2 = iosPerBiStr2;
        }
    }
}
